<script>
	import {
		mapState,
	} from "vuex";
	import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update'

	export default {
		async onLaunch(options) {
			// #ifdef H5
			try {
				const systemInfo = uni.getSystemInfoSync();
				this.$store.commit('login/changeAppVersion', systemInfo.appVersion)
				// #ifdef H5
				this.$store.commit('login/changeSystem', 'h5')
				this.$store.commit('login/changeOsName', 'h5')
				// await this.$store.dispatch('login/loginAction');
				// #endif
				// #ifdef APP-PLUS
				this.$store.commit('login/changeSystem', 'app')
				this.$store.commit('login/changeOsName', `app-${systemInfo.osName}`)
				uni.getPushClientId({
					success: (res) => {
						console.log("cid")
						console.log(res.cid);
					},
					fail(err) {
						console.log(err)
					}
				})
				// #endif
			} catch (error) {}
			// #endif

			// #ifdef APP-PLUS
			// APP端监听推送
			uni.onPushMessage((res) => {
				console.log("收到推送消息：", res)
				if (res.type == 'receive') {
					// 创建本地通知栏消息
					console.log('-receive-')
					console.log(res.data.payload)
					uni.createPushMessage({
						title: res.data.title,
						content: res.data.content,
						payload: res.data.payload,
						success: res => {
							console.log(res)
						},
						fail: () => {

						}
					})
				}
				if (res.type == 'click') {
					console.log('-click-')
					this.openDetailPage({
						content_id: res.data.payload.content_id
					})
				}
			})

			plus.navigator.closeSplashscreen();
			this.clearAppBadge();

			checkUpdate()
			console.log("检查版本")

			// 使用微信分享
			// uni.share({
			// 	provider: "weixin",
			// 	scene: "WXSceneSession",
			// 	type: 1,
			// 	summary: "我正在使用HBuilderX开发uni-app，赶紧跟我一起来体验！",
			// 	success: function (res) {
			// 		console.log("success:" + JSON.stringify(res));
			// 	},
			// 	fail: function (err) {
			// 		console.log("fail:" + JSON.stringify(err));
			// 	}
			// });
			// #endif
		},
		components: {

		},
		onShow() {
			// app端被唤起后跳转逻辑处理
			// let args = plus.runtime.arguments
			// if (args) {
			// 	let url = args.split("unif04e16c:/")[1]
			// 	// 判断是否存在token userinfo
			// 	// 跳转到url页面
			// 	uni.navigateTo({
			// 		url,
			// 		success: () => {
			// 			// 防止从跳转的页面返回上一页面后，将app切换至后台后，再次切换app显示时，又跳到h5唤起的那个页面
			// 			plus.runtime.arguments = ""
			// 		}
			// 	})
			// }
		},
		onHide() {},
		computed: {
			...mapState({
				userInfo: state => state.login.userInfo,
				token: state => state.login.token
			}),
		},
		methods: {
			// 点击推送通知唤起APP进入方案详情页
			openDetailPage(obj) {
				uni.navigateTo({
					url: `/pages/index/index`,
				});
			},
			clearAppBadge() {
				plus.runtime.setBadgeNumber(0);
			},
			// 获取系统栏高度
			async setAppInfo() {
				let that = this;
				return new Promise((resolve, reject) => {
					uni.getSystemInfo({
						success: function(e) {
							Vue.prototype.StatusBar = e.statusBarHeight;
							// #ifdef H5
							Vue.prototype.CustomBar = e.statusBarHeight + 45;
							// #endif

							// #ifdef APP-PLUS
							if (e.platform == "android") {
								Vue.prototype.CustomBar = e.statusBarHeight + 50;
							} else {
								Vue.prototype.CustomBar = e.statusBarHeight + 45;
							}
							// #endif

							// #ifdef MP-WEIXIN
							let custom = wx.getMenuButtonBoundingClientRect();
							Vue.prototype.Custom = custom;
							Vue.prototype.CustomBar =
								custom.bottom + custom.top - e.statusBarHeight;
							// #endif
						},
					});
				});
			},
		},
	};
</script>

<style lang="scss">
	/* 引入uView基础样式 */
	@import "@/uni_modules/uview-ui/index.scss";
	/* 每个页面公共css */
	@import "@/assets/css/index.scss";

	page {
		background-color: $uni-common-bg-color;
	}

	view {
		font-family: PingFang SC;
		box-sizing: border-box;
	}
</style>